App.require(["strTool"], function (strTool) {
    var app = App.getCurrent();

    app.definePage("strdateRangePickerSecond", {
        getDomObj: function () {
            this.attachDom(".start-time", "startTimeField")
                .attachDom(".start-time input", "startInput")
                .attachDom(".end-time", "endTimeField")
                .attachDom(".end-time input", "endInput")
                .attachEvent("endInput", "keydown", this.keyDownHandler, false)
                .attachEvent("endInput", "input", this.inputHandler, false)
                .attachEvent("startInput", "keydown", this.keyDownHandler, false)
                .attachEvent("startInput", "input", this.inputHandler, false);
        },
        keyDownHandler: function (ev) {
            if (!/\d/.test(ev.key) && ev.key != "Backspace" && ev.key != "ArrowLeft" && ev.key != "ArrowRight") ev.preventDefault();
            var value = ev.target.value;
            if (value.length == 9 && parseInt(value.slice(8)) == 3) {
                if (parseInt(ev.key) > 1) ev.preventDefault();
            }
        },
        getValue: function () {
            var input = this.domList.input, yDate = this.data.yDate, value = input.value;
            if (value.length == 10) {
                yDate.year = parseInt(value.slice(0, 4));
                yDate.month = parseInt(value.slice(5, 7));
                yDate.day = parseInt(value.slice(8, 10));
            }
        },
        inputHandler: function (ev) {
            var target = ev.target, value = target.value;
            value = ("" + value).replace(/-/g, "");
            if (value.length > 4) value = value.slice(0, 4) + "-" + value.slice(4);
            if (value.length > 7) value = value.slice(0, 7) + "-" + value.slice(7);
            if (value.length == 6) {
                var num = parseInt(value.slice(5));
                if (num > 1) value = value.slice(0, 5) + "0" + num + "-";
            }
            if (value.length == 7) {
                var num = parseInt(value.slice(5)), tNum = parseInt(num / 10), zNum = num % 10;
                if (num > 12) value = value.slice(0, 5) + "0" + tNum + "-" + zNum;
            }
            if (value.length == 9) {
                var num = parseInt(value.slice(8));
                if (num > 3) value = value.slice(0, 8) + "0" + num;
            }
            target.value = value;

            if (value.length === 10) {
                var isStart = this.domList.startTimeField.contains(target),
                    yDate = isStart ?  this.data.startYDate : this.data.endYDate;

                var year = parseInt(value.slice(0, 4)),
                    month = parseInt(value.slice(5, 7)),
                    day = parseInt(value.slice(8, 10));

                this._computedDate(yDate, year, month, day);
                target.value = [
                    yDate.year,
                    strTool.wrap(yDate.month, "0", 2),
                    strTool.wrap(yDate.day, "0", 2)
                ].join("-");

                this.parent.staticPage.dispatchEvent({ type: "changeDate", sign: isStart ? 0 : 1, string: yDate.toMonthDay() });
            }
        },
        _computedDate: function (yDate, year, month, day) {
            var data = this.data, minDate = data.minDate, maxDate = data.maxDate;
            yDate.year = year;
            yDate.month = month;
            yDate.day = day;
            if (minDate.year || maxDate.year) {
                if (maxDate.year && maxDate.compare(year, month, day) === -1) {
                    yDate.year = maxDate.year;
                    yDate.month = maxDate.month;
                    yDate.day = maxDate.day;
                }
                if (minDate.year && minDate.compare(year, month, day) === 1) {
                    yDate.year = minDate.year;
                    yDate.month = minDate.month;
                    yDate.day = minDate.day;
                }
            }
        },
        render: function (next) {
            var parentData = this.parent.data;
            this.data.startYDate = parentData.startYDate;
            this.data.endYDate = parentData.endYDate;
            this.data.minDate = parentData.minDate;
            this.data.maxDate = parentData.maxDate;
            this.fetch("./index.html", function (html) {
                next(html);
            })
        },
        init: function () {
            var startInput = this.domList.startInput, 
                endInput = this.domList.endInput,
                textField = this.domList.startTimeField,
                startYDate = this.data.startYDate,
                endYDate = this.data.endYDate;

            textField.classList.add("str-text-edit");
            textField.classList.add("str-text-color");
            startInput.focus();

            if (startYDate.year) {
                startInput.value = startYDate.toFormatString();
            }
            if (endYDate.year) {
                endInput.value = endYDate.toFormatString();
                this.domList.endTimeField.classList.add("str-text-edit");
            }
        },
    })
});